import {
  getcinemTop,
  getcinemMiddle,
  getcinemBottom
} from "../../api/mm"
import dayjs from "dayjs"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listTop: null,
    listmiddle: {},
    services: [],
    imgs: [],
    curretInfo: {},
    currentSchedule: [],
    actors: "",
    showDate: [],
    filmId: "", //电影id
    date: "" ,//需要请求的事件戳,
    cinemaId:""
  },

  // 放映数据函数
  async getcurrentSchedule(filmId, cinemaId, date) {
    this.setData({
      currentSchedule: (await getcinemBottom({
        filmId,
        cinemaId,
        date
      })).data.schedules.map((item) => {
        return {
          ...item,
          showAt: dayjs.unix(item.showAt).format("HH:mm"),
          endAt: dayjs.unix(item.endAt).format("HH:mm")
        }
      })
    })
  },

  // 海报图片被点击
  changeFilmId(e) {

    // 切换对应下标的电影、放映日期数据
    const index = e.currentTarget.dataset.index
    const filmId=e.currentTarget.dataset.id
    this.setData({
      filmId,
      curretInfo: this.data.listmiddle[index],
      date:this.data.listmiddle[index].showDate[0],
      actors: this.data.listmiddle[index].actors.map(item => item.name).join(" | "),
      showDate: this.data.listmiddle[index].showDate.map((item) => {
        return {
          data: dayjs.unix(item).format("MM月DD日"),
          unix: item
        }
      })
    })

    // 
    const pags = getCurrentPages()
    const cinmId = pags[pags.length - 1].options.id
    const date = this.data.data
    this.getcurrentSchedule(filmId, cinmId, date)
  },

  async fetaData(id) {
    const tempTop = (await getcinemTop({
      cinemaId: id
    })).data.cinema

    const tempMiddle = (await getcinemMiddle({
      cinemaId: id
    })).data.films

    // 获取放映数据
    const filmId = tempMiddle[0].filmId
    const date = tempMiddle[0].showDate[0]
    this.getcurrentSchedule(filmId, id, date)

    // 给data数据赋值
    this.setData({
      listTop: tempTop,
      listmiddle: tempMiddle,
      date:tempMiddle[0].showDate[0],
      filmId:filmId,
      cinemaId:id,
      
      // 对服务项目映射为数组对象
      services: tempTop?.services?.map(item => item?.name) || [],

      // 映射对应的海报数据
      imgs: tempMiddle.map((item) => {
        return {
          filmId: item.filmId,
          poster: item.poster
        }
      }),

      // 默认显示第一条数据信息
      curretInfo: tempMiddle[0],
      actors: tempMiddle[0].actors.map(item => item.name).join(" | "),

      // 默认渲染日期字段
      showDate: tempMiddle[0].showDate.map((item) => {
        return {
          data: dayjs.unix(item).format("MM月DD日"),
          unix: item
        }
      })
    })
  },

  changeUnix(e){
    const date=e.currentTarget.dataset.id
    this.setData({
      date
    })
    this.getcurrentSchedule(this.data.filmId, this.data.cinemaId, date)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetaData(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})